﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Xml;
using System.Xml.Schema;
using System.IO;
using DataTransferObject;

namespace DataService.TestingService
{
    public class TaskFromXML
    {
        public TaskList readTasks(String XMLPath)
        {
            TaskList taskList = new TaskList();
            
            XmlDocument xmldoc = new XmlDocument();
            //FileStream fs = new FileStream(@"D:\Jske\HOAN\tasks.xml", FileMode.Open, FileAccess.Read,
            //                   FileShare.ReadWrite);
            FileStream fs = new FileStream(XMLPath, FileMode.Open, FileAccess.Read,
                               FileShare.ReadWrite);
            xmldoc.Load(fs);

            XmlNodeList xmlnode = xmldoc.GetElementsByTagName("task");            

            for (int i = 0; i < xmlnode.Count; i++)
            {
                Task task = new Task();

                for (int j = 0; j < xmlnode[i].ChildNodes.Count; j++)
                {
                    string sName = xmlnode[i].ChildNodes[j].Name;
                    switch (sName)
                    {
                        case "startDate":
                            task.ScheduledStartDate = DateTime.ParseExact(xmlnode[i].ChildNodes[j].InnerText, "dd/MM/yyyy HH:mm", null);
                            break;
                        case "endDate":
                            task.ScheduledEndDate = DateTime.ParseExact(xmlnode[i].ChildNodes[j].InnerText, "dd/MM/yyyy HH:mm", null);
                            break;
                        case "priority":
                            task.Priority = Convert.ToInt32(xmlnode[i].ChildNodes[j].InnerText);
                            break;
                        case "totalWork":
                            task.TotalWork = Convert.ToInt32(xmlnode[i].ChildNodes[j].InnerText);
                            break;
                        case "isAssigned":
                            task.Completed = Convert.ToBoolean(xmlnode[i].ChildNodes[j].InnerText);
                            break;
                        case "remainTime":
                            task.RemainTime = Convert.ToInt32(xmlnode[i].ChildNodes[j].InnerText);
                            break;
                        case "name":
                            task.Name = xmlnode[i].ChildNodes[j].InnerText;
                            break;
                    }//end Switch

                    taskList.AddTaskToList(task);
                }//end For
            }
            return taskList;
        }//end readTasks      

        
    }
}
